package com.nuc.bigdata.app.service.impl;

import com.nuc.bigdata.app.service.AdminLoginService;
import com.nuc.bigdata.domain.entity.User;
import com.nuc.bigdata.enums.UserRoleEnum;
import com.nuc.bigdata.exception.BusinessException;
import com.nuc.bigdata.feign.LoginServiceFeign;
import com.nuc.bigdata.model.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpSession;

/**
 * @author JackLuo
 * @date 2019/10/22
 */
@Service
public class AdminLoginServiceImpl implements AdminLoginService {
    /**
     * feign接口初始化
     */
    private final LoginServiceFeign loginServiceFeign;

    @Autowired
    public AdminLoginServiceImpl(LoginServiceFeign loginServiceFeign) {
        this.loginServiceFeign = loginServiceFeign;
    }


    /**
     * 管理员登录
     *
     * @param loginName 登录名
     * @param password  登陆密码
     * @param session   session存储用户数据
     * @return 管理员数据
     */
    @Override
    public User login(String loginName, String password, HttpSession session) throws Exception{
        //如果登录名和密码正确就返回用户(还未判断用户角色)
        User user = loginServiceFeign.toLogin(loginName, password);
        //判断用户角色
        if (!StringUtils.isEmpty(user)) {
            if (UserRoleEnum.ADMIN.getRoleNumber().equals(user.getRole())) {
                session.setAttribute(UserRoleEnum.USER.getRoleMsg(), user);
                return user;
            }else {
                throw new BusinessException(ResultCode.USER_ACCOUNT_NOT_PERMISSION,user);
            }
        }
        return null;
    }
}
